angular.module('gateway.controllers', ['gateway.services'])

.controller('GatewayCtrl', function($scope,$rootScope,$state, $timeout,$meteor,AUTH_MSG,$ionicActionSheet) {

	$scope.doAction = 'login';

	$scope.step = 1;
	$scope.step1 = function() {
		$scope.step++;
	};
	$scope.step2 = function() {
	};

	$scope.signIn = function() {
		$state.go('app.home');
	};

	$scope.credentials = {
		username: '',
		password: ''
	};
	$scope.error = '';

	$scope.doLogin = function(credentials) {
		$meteor.loginWithPassword(credentials.username, credentials.password,function (err) {
			var currUser = Meteor.user();
			if(err){
				$scope.error = AUTH_MSG[err.reason];
				console.log(err);
				return;
      }
			setTimeout(function(){
				if(currUser.profile.roleName=="老板"){
					window.location.href='ceo.html';
	      }else if(currUser.profile.roleName=="店长"){
					window.location.href='manager.html';
	      }else if(currUser.profile.roleName=="销售员"){  // && firstLogin=true
					window.location.href='seller.html';
					// window.location.href='#/pwmodify';
	      }else if(currUser.profile.roleName=="仓管员"){  // && firstLogin=true
					window.location.href='whm.html';
					// window.location.href='#/pwmodify';
	      }else if(currUser.profile.roleName=="收银员"){  // && firstLogin=true
					window.location.href='casher.html';
					// window.location.href='#/pwmodify';
	      }else{
					$scope.error = '尚未授权登陆';
	      }
      },500);
		});
	};

	$scope.showSheet = function() {
		var hideSheet = $ionicActionSheet.show({
		  buttons: [
				{ text: '老板账号' },
	      { text: '店长账号' },
	      { text: '销售员账号' },
	      { text: '仓管账号' },
	      { text: '收银员账号' }
	    ],
	    cancelText: '取消',
	    cancel: function() {
	    	hideSheet();
	    },
			cssClass:'royal',
	    buttonClicked: function(index) {
				var credentials ={username:"",password : "888888"};
				switch (index) {
					case 0:
						console.log("0");
						credentials.username='bg';
						break;
					case 1:
						credentials.username='storemanager';
						break;
					case 2:
						credentials.username='sales';
						break;
					case 3:
						credentials.username='whm';
						break;
					case 4:
						credentials.username='casher';
						break;
					default:
						break;
				}
				$scope.doLogin(credentials);
	    	return true;
	    }
	  });
	};
});
